package cn.iocoder.yudao.module.aiposter.queue.draw;

import cn.iocoder.yudao.module.aiposter.config.PosterConfig;
import cn.iocoder.yudao.module.aiposter.config.RemoteConfig;
import cn.iocoder.yudao.module.aiposter.dal.dataobject.poster.PosterDO;
import cn.iocoder.yudao.module.aiposter.queue.Worker;
import cn.iocoder.yudao.module.aiposter.remote.DrawClient;
import cn.iocoder.yudao.module.aiposter.remote.ImgClient;
import com.github.yulichang.toolkit.SpringContentUtils;

import java.nio.file.Path;

public class DrawCommand implements Runnable {

    private Worker<DrawResult> worker;
    private PosterDO posterDO;
    private DrawTypeEnum type;
    private DrawClient drawClient;
    private ImgClient imgClient;

    public DrawCommand(Worker<DrawResult> worker, PosterDO posterDO, DrawTypeEnum type) {
        this.worker = worker;
        this.posterDO = posterDO;
        this.type = type;
        this.drawClient = new DrawClient(SpringContentUtils.getBean(PosterConfig.class));
        this.imgClient = new ImgClient(SpringContentUtils.getBean(RemoteConfig.class), SpringContentUtils.getBean(PosterConfig.class));
    }

    @Override
    public void run() {
        String resultUrl = null;
        if (type.equals(DrawTypeEnum.COVER)) {
            Path drawImgUrl = drawClient.coverScreenshot(posterDO.getId());
            resultUrl = imgClient.uploadCover(drawImgUrl.toString());
        }else if(type.equals(DrawTypeEnum.POSTER)){
            Path drawImgUrl = drawClient.posterScreenshot(posterDO.getId(), posterDO.getWidth(), posterDO.getHeight());
            resultUrl = imgClient.uploadPoster(drawImgUrl.toString());
        }
        worker.setResult(new DrawResult(resultUrl));
    }
}
